//
//  Inherit.swift
//  SwiftDemo
//
//  Created by lizq on 16/7/6.
//  Copyright © 2016年 lizq. All rights reserved.
//

import UIKit


struct Fahrenheit {
    var temperature: Double
    var value = 10
    init() {
        temperature = 0.0
    }
    init(temperature value: Double) {
        temperature = value
    }
}

class Vehicle {
     var privateValue: Double
     var currentSpeed: Double
    
    init(privateValue:Double) {
        self.privateValue = privateValue
        currentSpeed = 50
    }
    
    deinit {
        
    }
    
    var description : String {
        return "traveling at \(currentSpeed) miles per hour"
    }
    func makeNoise() {
        // todo
        print("I'am a Vehicle")
    }
    
    final func finalFunc() {
        print("You can't override finalFunc")
    }
}

class Bicycle: Vehicle {
    var hasBasket:Bool?
    var isAuto:Bool?
    init(hasBasket:Bool) {
        super.init(privateValue: 20)
        self.hasBasket = hasBasket
    }
    deinit {
        print("Bicycle deinit")
    }
    override var description: String {
        return super.description + "in Bicyle"
    }
    override func makeNoise() {
        super.makeNoise()
        print("I'am a bicycle")
    }
}

final class FinalClass {
    func action() {
        print("this is a final class")
    }
}


class Person {
    var name: String
    var apartment: Apartment?
    init(name: String) {
        self.name = name
        print("\(name) is being initialized")
    }
    
    deinit {
        print("Person is being deinitialized")
    }
}

class Apartment {
    let number: Int
    weak var tenant: Person?
    init(number:Int) {
        self.number = number
    }
    deinit {
        print("Apartment deinit")
    }
}


extension Int {
    // 扩展 extension
    /* 
        extension ClassName {
            //todo
        }
     */
    // 扩展 计算型属性
    var km: Int { return self * 1000}
    var m: Int { return self}
    // 扩展构造器
    // 扩展 方法
    func discript(_ task:()->()) {
        for i in 1...self {
            task()
        }
    }
    // 扩展 嵌套类型
    enum Kind {
        case negative,zero,positive
    }
    var kind:Kind {
        switch self {
        case 0:
            return .zero
        case let x where x > 0:
            return .positive
        default :
            return .negative
        }
    }
}

class Inherit: NSObject {
    
    deinit {
        print("a inherit init...");
    }
    var obj = NSObject();
    func action() {
        var bicycle: Bicycle?
        bicycle = Bicycle(hasBasket: false)
        bicycle?.privateValue
        print(bicycle)
        let temperature2 = Fahrenheit(temperature: 10)
        
        var reference1: Person?
        var reference2: Person?
        var reference3: Person?
        
        reference1 = Person(name: "Person 1")
        reference1?.name = "Hello world"
        reference2 = reference1
        reference3 = reference1
        reference3 = nil
        reference2 = nil
        print("deinit")
        print(reference1)
        bicycle = nil
        reference1 = nil
        
        
        var person:Person?
        var apartment: Apartment?
        person = Person(name:"Jon")
        apartment = Apartment(number: 102)
        //////
        person?.apartment = apartment
        apartment?.tenant = person
        
        // 类型转换
        // is 用类型检查符is 来检查一个实例是否属于特定子类型，属于返回 true 否则返回false
        if person is Person? {
            print("person is Person Type")
        }
        // as
        // as?  可以能是
        // as!  一定是
        
        3.discript({ () -> () in
          print("hello 3")
        })
        print(4.kind)
        print((-4).kind)
        print(0.kind)
    }
    
    fileprivate func privateFunc() {
        print("this is a private func.")
    }
    
    internal func publicFunc() {
        print("this is a public func.")
    }
}

internal class  InternalClass {

}

private class PrivateClass {

}

open class PublicClass {
    
}

